package com.kgc.springboot.aviation.service.jianghongkun.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.springboot.aviation.mapper.ConsultingMapper;
import com.kgc.springboot.aviation.mapper.LawsMapper;
import com.kgc.springboot.aviation.pojo.Consulting;
import com.kgc.springboot.aviation.pojo.Laws;
import com.kgc.springboot.aviation.pojo.LawsExample;
import com.kgc.springboot.aviation.service.jianghongkun.LawsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: LawsServiceImpl <br/>
 * Description: 法规接口实现类<br/>
 * date: 2020/12/29 15:36<br/>
 *
 * @author 姜洪坤<br />
 * @since JDK 1.8
 */
@Service
public class LawsServiceImpl implements LawsService {
    //@Resource依赖注入，从容器中自动获取bean
    @Resource
    LawsMapper lawsMapper;
    @Resource
    ConsultingMapper consultingMapper;

    /**
     * Description: 分页遍历所有数据，条件查询方法
     *              根据法规部号 法规名称<br/>
     * @author: 姜洪坤<br/>
     * @date: 2020/12/29 15:20<br/>
     * @param:<br/>
     * @return:
     */
    @Override
    public PageInfo<Laws> getLawsList( String lawNum, String lawName, Integer pageNum, Integer pageSize) {
        System.out.println("进入到查询法规方法");
        System.out.println("从前端获取的法规部号为"+lawNum);
        System.out.println("从前端获取的法规名称为"+lawName);
        //设置起始页，页容量
        PageHelper.startPage(pageNum, pageSize);
        //集合中 法规为空
        List<Laws> lawsList = null;
        //进行条件查询--分三种情况
        //第一种
        //根据法规部号查找，法规名称为空
        //第二种
        //根据法规名称查找，法规部号为空
        //第三种
        //根据法规部号  和  法规名称查找
        //且第一次进入页面，默认查询所有

        LawsExample lawsExample = new LawsExample();
        LawsExample.Criteria criteria = lawsExample.createCriteria();
        //第一种情况
        if ((lawNum != null && lawNum.length() > 0) && (lawName == null || lawName.equals(""))) {
            System.out.println("进入到第一种情况");
            criteria.andPartNumAliasLike("%"+lawNum+"%");
        }
        //第二种情况
        if ((lawNum == null || lawNum.equals("")) && (lawName != null && lawName.length() > 0)){
            System.out.println("进入到第二种情况");
            criteria.andLawsNameLike("%"+lawName+"%");
        }
        //第三种情况
        if ((lawNum != null && lawNum.length() > 0) && (lawName != null && lawName.length() >0)){
            System.out.println("进入到第三种情况");
            criteria.andPartNumAliasLike("%"+lawNum+"%").andLawsNameLike("%"+lawName+"%");
        }
        //第四种情况
        if ((lawNum == null && lawNum.length() == 0) && (lawName == null && lawName.length() ==0)){
            System.out.println("进入到第四种情况");
            //默认为空
            lawsExample=null;
        }
        System.out.println("开始查询");
        System.out.println(lawsExample);
        List<Laws> laws = lawsMapper.selectByExample(lawsExample);
        if (laws.size() > 0 && laws != null) {
            System.out.println("查询结果不为空");
            PageInfo<Laws> pageInfo = new PageInfo<>(laws);
            return pageInfo;
        }
        System.out.println("查询结果为空");
        return null;
    }

    /**
     * Description: 删除方法，删除一条法规，根绝id<br/>
     * @author: 姜洪坤<br/>
     * @date: 2020/12/29 16:16<br/>
     * @param:<br/>
     * @return:
     */
    @Override
    public int delLaw(Integer id) {
        int i = lawsMapper.deleteByPrimaryKey(id);
        if (i > 0){
            //删除成功
            return i;
        }
        return 0;
    }

    /**
     * Description: 修改方法，根据修改后的对象<br/>
     * @author: 姜洪坤<br/>
     * @date: 2020/12/29 16:20<br/>
     * @param:<br/>
     * @return:
     */
    @Override
    public int addlaw(Laws law) {
        int i = lawsMapper.insert(law);
        if (i > 0){
            //修改成功
            return i;
        }
        return 0;
    }

    @Override
    public int update(Laws laws) {

        int i = lawsMapper.updateByPrimaryKey(laws);

        if (i>0){
            return i;
        }

        return 0;
    }

    @Override
    public Laws selectByKey(Integer integer) {

        Laws laws = lawsMapper.selectByPrimaryKey(integer);
        return laws;
    }

    @Override
    public int dellaw(Integer integer) {

        int i = lawsMapper.deleteByPrimaryKey(integer);


        return i;
    }

    @Override
    public int addConsulting(Consulting consulting) {

        int insert = consultingMapper.insert(consulting);


        return insert;
    }

    @Override
    public List<Laws> getAll() {
        LawsExample example=new LawsExample();
        List<Laws> laws = lawsMapper.selectByExample(example);
        return  laws;
    }

    @Override
    public List<Laws> selectAll(String lawsName) {
        LawsExample example=new LawsExample();
        if (lawsName!=null&lawsName.length()>0){
            example.createCriteria().andLawsNameEqualTo(lawsName);
          return  lawsMapper.selectByExample(example);
        }
        return null;
    }

}
